package com.example.blog.controller;

import com.example.blog.domain.Response;
import com.example.blog.note.NoToken;
import com.example.blog.schedulerJob.TestJob;
import com.example.blog.schedulerJob.UserInfoScheduledCleanupJob;
import com.example.blog.dto.publicDto.QuartzRepeatDto;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

//定时任务
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    private final Scheduler scheduler;
    @Autowired
    public QuartzController(Scheduler scheduler){
        this.scheduler=scheduler;
    }

    @NoToken
    @GetMapping("/start1")
    public Response start() throws SchedulerException {
        String orderNo = "orderNo";
        Date start = new Date(System.currentTimeMillis() + 3 * 100);
        Date end = new Date(System.currentTimeMillis() + 3600 * 24);
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                /**给当前JobDetail添加参数，K V形式*/
                .usingJobData("name","zy")
                /**给当前JobDetail添加参数，K V形式，链式调用，可以传入多个参数，在Job实现类中，可以通过jobExecutionContext.getJobDetail().getJobDataMap().get("age")获取值*/
                .usingJobData("id",1001)
                /**添加认证信息，有3种重写的方法，我这里是其中一种，可以查看源码看其余2种*/
                .withIdentity(orderNo)
                .build();//执行
        Trigger trigger = TriggerBuilder.newTrigger()
                 .usingJobData("orderNo", orderNo)
                .withIdentity(orderNo)
                .startAt(start)
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                                .withIntervalInSeconds(3)
                                .repeatForever()
                )
                .build();//执行
        scheduler.scheduleJob(jobDetail, trigger);
        if(!scheduler.isShutdown()){
                scheduler.start();
                System.out.println(scheduler.isShutdown());
        }
        return new Response(Response.SUCCEED_CODE,"定时任务1",true);
    }
    @NoToken
    @GetMapping("/repeat")
    public Response start1(@Validated @RequestBody QuartzRepeatDto quartzRepeatVo) throws SchedulerException {
        String orderNo = "orderNo2";
        Date start = new Date(System.currentTimeMillis() + 3 * 100);
        Date end = new Date(System.currentTimeMillis() + 3600 * 24);
        JobDetail jobDetail = JobBuilder.newJob(UserInfoScheduledCleanupJob.class)
                /**给当前JobDetail添加参数，K V形式*/
                .usingJobData("name","zy2")
                /**给当前JobDetail添加参数，K V形式，链式调用，可以传入多个参数，在Job实现类中，可以通过jobExecutionContext.getJobDetail().getJobDataMap().get("age")获取值*/
                .usingJobData("id",1002)
                /**添加认证信息，有3种重写的方法，我这里是其中一种，可以查看源码看其余2种*/
                .withIdentity(quartzRepeatVo.getLogotype())
                .build();//执行
        CronTrigger trigger = TriggerBuilder.newTrigger()
                //认证信息
                .usingJobData("logotype",quartzRepeatVo.getLogotype())
                .withIdentity(quartzRepeatVo.getLogotype())
                .startAt(start)
                .endAt(end) //一天过后没有执行，则不再执行
                //"0 26 12 * * ?"
                .withSchedule(CronScheduleBuilder.cronSchedule(quartzRepeatVo.getStartTime()))
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        if(!scheduler.isShutdown()){
            scheduler.start();
            System.out.println(scheduler.isShutdown());
        }
        return new Response(Response.SUCCEED_CODE,"定时任务",true);
    }
    @NoToken
    @GetMapping("/stop")
    public Response stop() throws SchedulerException {
        scheduler.pauseTrigger(TriggerKey.triggerKey("orderNo"));
        return new Response(Response.SUCCEED_CODE,"停止定时任务",true);
    }
    @NoToken
    @GetMapping("/resume")
    public Response resume() throws SchedulerException {
        scheduler.resumeTrigger(TriggerKey.triggerKey("orderNo"));
        return new Response(Response.SUCCEED_CODE,"恢复定时任务",true);
    }
    @NoToken
    @GetMapping("/del")
    public Response del() throws SchedulerException {
        scheduler.pauseTrigger(TriggerKey.triggerKey("orderNo2")); //暂停
        scheduler.unscheduleJob(TriggerKey.triggerKey("orderNo2")); //移除
        scheduler.deleteJob(JobKey.jobKey("orderNo2"));
        return new Response(Response.SUCCEED_CODE,"删除定时任务",true);
    }
}
